package com.sen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sen.config.security.JwtTokenUtil;
import com.sen.config.security.MyUserDetailService;
import com.sen.dto.AccountDTO;
import com.sen.entity.Account;
import com.sen.mapper.AccountMapper;
import com.sen.result.Result;
import com.sen.service.AccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sen
 * @since 2021-03-13
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private MyUserDetailService userDetailService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    //token的头部信息
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    @Override
    public Account getAccountByUsername(String username) {
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        Account account = accountMapper.selectOne(wrapper);
        return account;
    }

    /**
     * 新增一个用户
     * @param accountDTO
     * @return
     */
    @Override
    public Result saveAccount(AccountDTO accountDTO) {
        String username = accountDTO.getUsername();
        if (!ObjectUtils.isEmpty(getAccountByUsername(username))) {
            return Result.error().msg("该用户名已被注册！");
        }
        String password = accountDTO.getPassword();
        password= passwordEncoder.encode(password);
        accountDTO.setPassword(password);
        Account account = new Account();
        BeanUtils.copyProperties(accountDTO, account);
        this.save(account);
        return Result.ok().msg("注册成功！");
    }

    /**
     * 分页查询所有用户信息列表
     * @param cur
     * @param size
     * @return
     */
    @Override
    public Result getAccountList(Integer cur, Integer size) {
        Page<Account> page = new Page<>(cur, size);
        Page<Account> accountPage = accountMapper.selectPage(page, null);
        List<Account> accountList = accountPage.getRecords();
        for (Account account : accountList){
            account.setPassword(null);
        }
        return Result.ok().data("data", accountList);
    }
}
